﻿using BootstrapBlazor.Components;
using OnWork.DataCenter.BLL.Base.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mapster;

namespace OnWork.DataCenter.Shared.Common
{
    /// <summary>
    /// 视图模型扩展
    /// </summary>
    public static class ViewModelExtensions
    {
        /// <summary>
        /// 视图查询对象转webapi查询对象
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public static BaseQueryPageInput ToBaseQueryPageInput(this QueryPageOptions option)
        {
            if (option != null)
            {
                BaseQueryPageInput data = new BaseQueryPageInput()
                {
                    //Filters = new List<BaseFilterKeyValue>(),
                    IsPage = option.IsPage,
                    PageIndex = option.PageIndex,
                    PageItems = option.PageItems,
                    SearchModel = option.SearchModel,
                    //Searchs = new List<BaseFilterKeyValue>(),
                    SearchText = option.SearchText,
                    SortName = option.SortName,
                    //SortOrder = option.SortOrder,
                };

                switch (option.SortOrder)
                {
                    case SortOrder.Unset:
                        data.SortOrder = BaseSortOrder.Unset;
                        break;

                    case SortOrder.Asc:
                        data.SortOrder = BaseSortOrder.Asc;
                        break;

                    case SortOrder.Desc:
                        data.SortOrder = BaseSortOrder.Desc;
                        break;

                    default:
                        break;
                }

                //查询条件
                if (option.Searches != null)
                {
                    foreach (var item in option.Searches)
                    {
                        var search = new List<BaseFilterKeyValue>();
                        //foreach (var filter in item.GetFilterConditions())
                        //{
                        //    var val = new BaseFilterKeyValue()
                        //    {
                        //        FieldKey = filter.FieldKey,
                        //        FieldValue = filter.FieldValue.ToString(),
                        //        FilterLogic = BaseFilterLogic.Or, //查询永远是或的关系
                        //        FilterAction = (BaseFilterAction)((int)filter.FilterAction)
                        //    };
                        //    search.Add(val);
                        //}
                      
                        data.Searchs.Add(search);
                    }
                }

                //筛选条件
                if (option.Filters != null)
                {
                    foreach (var item in option.Filters)
                    {
                        var filterlist = new List<BaseFilterKeyValue>();
                        //foreach (var filter in item.GetFilterConditions())
                        //{
                        //    var val = new BaseFilterKeyValue()
                        //    {
                        //        FieldKey = filter.FieldKey,
                        //        FieldValue = filter.FieldValue.ToString(),
                        //        FilterLogic = (BaseFilterLogic)((int)filter.FilterLogic),
                        //        FilterAction = (BaseFilterAction)((int)filter.FilterAction)
                        //    };
                        //    filterlist.Add(val);
                        //}
                        data.Filters.Add(filterlist);
                    }
                }
                return data;
            }
            return null;
        }

        /// <summary>
        /// PageList 转换为 QueryData
        /// </summary>
        /// <typeparam name="T">原始类型</typeparam>
        /// <typeparam name="R">返回类型</typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static QueryData<R> ToQueryData<R, T>(this SQLReturn<List<T>> input, bool isFiltered = true) where R : class, new()
        {
            return new QueryData<R>()
            {
                TotalCount = input.Total,
                Items = input.Data.Adapt<List<R>>(),
                IsFiltered = isFiltered,
            };
        }

        /// <summary>
        /// Date转换未 DateTimeOffSet
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static DateTimeOffset ToDateTimeOffset(this DateTime input, DateTimeKind dateTimeKind = DateTimeKind.Local)
        {
            return DateTime.SpecifyKind(input, dateTimeKind);
        }

    }
}
